﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Electronic_Catalog_XML_Generator_Libs.models;


namespace Electronic_Catalog_XML_Generator_Libs.tools
{
    public class FileTools
    {
        public string? FileDirectory { get; } = null;
        public string? Fileter { get; } = null;
        public Action<string, string> Notifys { get; set; }
        //文件列表
        public Dictionary<string, FileModel>? Files { get; private set; } = null;
        public FileTools(string directory, string fileter)
        {
            FileDirectory = directory;
            Fileter = fileter;
        }
        //加载文件信息
        public void LoadFiles()
        {
            Files = new Dictionary<string, FileModel>();
            if (FileDirectory == null)
                throw new ArgumentNullException(nameof(FileDirectory));
            if (Fileter == null)
                throw new ArgumentNullException(nameof(Fileter));
            var files = Directory.GetFiles(FileDirectory, Fileter);
            for (int i = 0; i < files.Length; i++)
            {
                FileInfo fileInfo = new FileInfo(files[i]);
                FileModel model = new FileModel();
                var filename = Path.GetFileNameWithoutExtension(files[i]);
                model.Name = filename;
                model.Description = "Desc";
                model.Type = "Layouts";
                model.Manufacturer = "Layouts";
                model.MaxPayLoad = "0";
                model.Reach = "0";
                model.Author = "Author";
                model.Email = "example@email.com";
                model.Website = "WebSite";
                model.DetailedRevision = "4.5.0.30";
                model.Revision = "30";
                model.VcId = "";
                model.IsDeprecated = "false";
                model.Tags = "";
                model.File = files[i];
                model.Icon = model.Icon + "/" + model.Name?.Trim() + "_Icon" + ".png";
                model.PreviewIcon = model.PreviewIcon?.Trim() + "/" + model.Name + "_Preview" + ".png";
                model.DateModified = fileInfo.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
                model.FileSize = fileInfo.Length.ToString();
                model.ModelType = "Layout";
                model.NameAndExc = "/" + Path.GetFileName(files[i]);
                model.FileFullPath = Path.GetFullPath(files[i]);
                model.NotifyPropertyChanged = Notifys;
                Files.Add(filename, model);
            }
        }
    }
}
